package c_admin

import (
	"gintest/models"
	"gintest/until"
	"github.com/gin-gonic/gin"
	"strings"
)

func Login_dologin(c *gin.Context){
	username := c.DefaultPostForm("username", "")
	password := c.DefaultPostForm("password", "")
	if len(username)==0 {
		until.Fail(c,1,"账号不能为空")
		return
	}
	if len(password)==0 {
		until.Fail(c,1,"密码不能为空")
		return
	}
	authuser:=models.GetAuthUserForUsername(username)
	if authuser==(models.AuthUser{}) {
		until.Fail(c,1,"用户不存在")
		return
	}

	if !strings.EqualFold(authuser.PassWord,password) {
		until.Fail(c,1,"密码错误")
		return
	}
	if authuser.Status!=1 {
		until.Fail(c,1,"账号未启用")
		return
	}

	token:=until.Token{
		Id:authuser.Id,
		UserName:authuser.UserName,
		PassWord:authuser.PassWord,
		CreateAt:authuser.CreateAt,
		UpdateAt:authuser.UpdateAt,
		Status:authuser.Status,
	}
	token_key:=until.PutToken(token,"ses_")

	until.Success(c,gin.H{"token":token_key,"id":token.Id,"username":token.UserName})
}